﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.ScriptEngine.Debuger;
using System;
using System.Activities;
using System.Activities.Tracking;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.ScriptEngine.Custom
{
    /// <summary>
    /// 调试追踪器
    /// </summary>
    internal class DebugTrackingParticipant : System.Activities.Tracking.TrackingParticipant
    {
        private const String all = "*";

        private WorkflowDebuger _workflowDebuger = null;

        public DebugTrackingParticipant(WorkflowDebuger workflowDebuger)
        {
            _workflowDebuger = workflowDebuger;
            //追踪配置
            TrackingProfile = new TrackingProfile()
            {
                Name = "DebugTracking",
                ImplementationVisibility = ImplementationVisibility.All,
                Queries = {
                    new CustomTrackingQuery()
                    {
                        Name = all,
                        ActivityName = all
                    },
                    new WorkflowInstanceQuery()
                    {
                        // Limit workflow instance tracking records for started and completed workflow states
                        States = { WorkflowInstanceStates.Started, WorkflowInstanceStates.Completed, WorkflowInstanceStates.Idle },
                    },
                    new ActivityStateQuery()
                    {
                        // Subscribe for track records from all activities for all states
                        ActivityName = all,
                        States ={ ActivityStates.Executing, ActivityStates.Closed, ActivityStates.Canceled, ActivityStates.Faulted },

                        // Extract workflow variables and arguments as a part of the activity tracking record
                        // VariableName = "*" allows for extraction of all variables in the scope
                        // of the activity
                        Variables = { all },
                        Arguments ={ all }
                    },
                    new ActivityScheduledQuery
                    {
                        ActivityName = all,
                        ChildActivityName = all
                    }
                }
            };

            _workflowDebuger.SetTrackingProfile(TrackingProfile);
        }

        protected override void Track(TrackingRecord record, TimeSpan timeout)
        {
            OnTrackingRecordReceived(record, timeout);
        }

        protected void OnTrackingRecordReceived(TrackingRecord record, TimeSpan timeout)
        {
            var activityScheduledRecord = record as ActivityScheduledRecord;
            if (activityScheduledRecord != null && activityScheduledRecord.Activity != null && activityScheduledRecord.Child != null)
            {
                _workflowDebuger.ReceiveTrackRecord(activityScheduledRecord);
            }

            ActivityStateRecord activityStateRecord = record as ActivityStateRecord;
            if ((activityStateRecord != null) && (!activityStateRecord.Activity.TypeName.Contains("System.Activities.Expressions")))
            {
                _workflowDebuger.ReceiveTrackRecord(activityStateRecord);
            }
        }
    }
}
